# SonarQube 快速扫描脚本 - PowerShell 版本
# 学生请假管理系统

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  SonarQube 快速扫描" -ForegroundColor Cyan
Write-Host "  学生请假管理系统" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# ⚠️ 在这里设置你的 Token
$SONAR_TOKEN = "YOUR_TOKEN_HERE"

# 检查是否已替换 Token
if ($SONAR_TOKEN -eq "YOUR_TOKEN_HERE") {
    Write-Host "[错误] 请先编辑此文件，将 YOUR_TOKEN_HERE 替换为你的真实 Token" -ForegroundColor Red
    Write-Host ""
    Write-Host "获取 Token 的步骤：" -ForegroundColor Yellow
    Write-Host "  1. 访问 http://localhost:9000"
    Write-Host "  2. 登录并创建项目"
    Write-Host "  3. 生成 Token 并复制"
    Write-Host "  4. 右键编辑此脚本文件，替换 YOUR_TOKEN_HERE"
    Write-Host ""
    Write-Host "或者在命令行直接传入 Token："
    Write-Host '  $env:SONAR_TOKEN = "你的token"' -ForegroundColor Green
    Write-Host '  .\快速扫描.ps1' -ForegroundColor Green
    Write-Host ""
    Read-Host "按回车键退出"
    exit 1
}

# 使用环境变量（如果已设置）
if ($env:SONAR_TOKEN) {
    $SONAR_TOKEN = $env:SONAR_TOKEN
    Write-Host "[✓] 使用环境变量中的 Token" -ForegroundColor Green
} else {
    Write-Host "[✓] Token 已设置" -ForegroundColor Green
}
Write-Host ""

# 检查 SonarQube 服务
Write-Host "[1/4] 检查 SonarQube 服务..." -ForegroundColor Yellow
try {
    $response = Invoke-WebRequest -Uri "http://localhost:9000/api/system/status" -UseBasicParsing -TimeoutSec 5 -ErrorAction Stop
    Write-Host "[✓] SonarQube 运行中" -ForegroundColor Green
} catch {
    Write-Host "[X] 无法连接到 SonarQube" -ForegroundColor Red
    Write-Host ""
    Write-Host "请确保 SonarQube 已启动：" -ForegroundColor Yellow
    Write-Host "  cd D:\sonarqube-25.10.0.114319\bin\windows-x86-64"
    Write-Host "  .\StartSonar.bat"
    Write-Host ""
    Read-Host "按回车键退出"
    exit 1
}
Write-Host ""

# 清理缓存
Write-Host "[2/4] 清理旧缓存..." -ForegroundColor Yellow
if (Test-Path ".scannerwork") {
    Remove-Item -Recurse -Force ".scannerwork"
}
Write-Host "[✓] 清理完成" -ForegroundColor Green
Write-Host ""

# 生成测试覆盖率
Write-Host "[3/4] 生成测试覆盖率..." -ForegroundColor Yellow
Push-Location server
try {
    npm run test:coverage
    Write-Host "[✓] 测试完成" -ForegroundColor Green
} catch {
    Write-Host "[警告] 测试失败，继续扫描..." -ForegroundColor Yellow
} finally {
    Pop-Location
}
Write-Host ""

# 运行扫描
Write-Host "[4/4] 运行扫描..." -ForegroundColor Yellow
Write-Host ""

# 检查是否安装了 sonar-scanner
$scannerInstalled = Get-Command sonar-scanner -ErrorAction SilentlyContinue

if ($scannerInstalled) {
    Write-Host "使用已安装的 sonar-scanner..." -ForegroundColor Cyan
    sonar-scanner -Dsonar.host.url=http://localhost:9000 -Dsonar.token=$SONAR_TOKEN
} else {
    Write-Host "使用 npx 运行扫描..." -ForegroundColor Cyan
    npx sonarqube-scanner -Dsonar.host.url=http://localhost:9000 -Dsonar.token=$SONAR_TOKEN
}

if ($LASTEXITCODE -eq 0) {
    Write-Host ""
    Write-Host "========================================" -ForegroundColor Green
    Write-Host "  ✓ 扫描完成！" -ForegroundColor Green
    Write-Host "========================================" -ForegroundColor Green
    Write-Host ""
    Write-Host "查看报告：" -ForegroundColor Cyan
    Write-Host "http://localhost:9000/dashboard?id=student-leave-management-system" -ForegroundColor Yellow
    Write-Host ""
    
    $openBrowser = Read-Host "是否打开浏览器查看报告？(Y/n)"
    if ($openBrowser -ne 'n' -and $openBrowser -ne 'N') {
        Start-Process "http://localhost:9000/dashboard?id=student-leave-management-system"
    }
} else {
    Write-Host ""
    Write-Host "[X] 扫描失败，请检查上面的错误信息" -ForegroundColor Red
    Write-Host ""
    Read-Host "按回车键退出"
    exit 1
}

